Load and populate list of Person (without children), filter and sort
XDocument data = XDocument.Load("persons.xml");
// select persons
List<Person> people = (
from p in data.Descendants("person")
where (int)p.Attribute("id") < 3
orderby (string)p.Element("name")
select new Person()
{
Id = (int)p.Attribute("id"),
Name = (string)p.Element("name"),
}
).ToList();
More info and samples on: www.devarchweb.net
Load and populate list of Person (with children)// select persons with children (nested elements)
List<Person> people1 = (
from p in data.Descendants("person")
select new Person()
{
Id = (int)p.Attribute("id").Value,
Name = (string)p.Element("name"),
Children = (
from ch in p.Descendants("child")
select new Child()
{
Name = (string)ch.Element("name"),
Age = (int)ch.Element("age")
}
).ToList()
}
).ToList();
More info and samples on: www.devarchweb.net
Load and populate children for persons with name 'John'// select nested elements only - children
List<Child> children = (from ch in config.Descendants("child")
where (string)ch.Parent.Parent.Element("name") == "John"
select new Child
{
Name = (string)ch.Element("name"),
Age = (int)ch.Element("age")
}
).ToList();
More info and samples on: www.devarchweb.net
How to read simple list of items Items can be read from a simple list<items>
<item>1</item>
<item>2</item>
<items>
List<string> result = (from i in data.Descendants("item")
select i.Value)
.ToList();